﻿using DLC.Game.DataTable;
using DLC.Game.DataTable.ArchiveString;
using DLC.Module.DataTable;
using DLC.Module.DataTable.ArchiveString;
using DLC.Module.DataTable.Default;
//using DLC.Game.DataTable;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
//using DLC.Game.DataTable.ArchiveScriptableObject;

namespace DLC.Example.String
{
	public class LoadDataTables : MonoBehaviour
	{
		public readonly IDataTableManager DataTableManager = new DataTableManager();

		public List<AssetReference> DataTableAssetRefs;
		[SerializeField]
		public Dictionary<string, AssetReference> DataTableAssetTypeRefs;

		void Start()
		{
			foreach (var dataAR in DataTableAssetRefs)
			{
				dataAR.LoadAssetAsync<TextAsset>().Completed += OnDataTableLoaded;
			}
		}

		AssetReference GetAssetReference(AsyncOperationHandle<TextAsset> obj)
		{
			foreach (var dataAR in DataTableAssetRefs)
			{
				if (dataAR.OperationHandle.Equals(obj))
				{
					return dataAR;
				}
			}
			return null;
		}

		private void OnDataTableLoaded(AsyncOperationHandle<TextAsset> obj)
		{
			Debug.Log($"OnDataTableLoaded......{obj.Result.GetType()}");
			var da = GetAssetReference(obj);
			Debug.Log($"Asset.name......{da.Asset.name}");
			var ss = da.Asset.name.Split('_');

			DataTableManager.CreateDataTable(ss[0], obj.Result.text);

            var table = DataTableManager.GetDataTables(ss[0]);
			foreach(var tt in table)
            {
				Debug.Log($"Get Data Table table is {tt}");
			}
        }
	}
}